package com.chushouya.order.service.admin.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.order.constants.*;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.lang.Dates;
//=====order.constants.*;
import com.chushouya.order.constants.*;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dto.admin.action.OrderAction;
import com.chushouya.order.service.admin.KefuOrderActionCtrlService;
import com.chushouya.order.util.KefuOrderActionUtil;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class KefuOrderActionCtrlServiceImpl implements KefuOrderActionCtrlService {

    @Resource
    private OrderRepository orderRepository;

    private OrderEntity getOrder(Long orderId) {
        final OrderEntity orderEntity = orderRepository.selectByPrimaryKey(orderId);
        Check.notNull(orderEntity, "订单不存在");
        return orderEntity;
    }

    /**
     * 订单操作许可验证
     */
    @Override
    public void actionExecuteCheck(KefuOrderActionEnum orderAction, OrderEntity orderEntity) {
//        log.info("订单操作许可验证：{}", Jsons.toJsonString(orderAction));
        // 订单xxx，不能执行xxx
        OrderStatusEnum orderStatus = OrderStatusEnum.get(orderEntity.getOrderStatus());
        Boolean allowExecute = Objects.nonNull(this.getActionList(orderEntity).stream().filter(f -> Objects.equals(f.getActionCode(), orderAction.getActionCode())).findFirst().orElse(null));
        Check.notFalse(allowExecute, "[{}]状态订单不可操作[{}]", orderStatus.named(), orderAction.named());
    }

    @Override
    public List<OrderAction> orderActions(Long orderId) {
//        if (Objects.equals(Contexts.getUserContext().getUserType(), ContextUserType.ADMIN_PLATFORM.value())) {
//            return Lists.emptyList();
//        }
        return this.getActionList(this.getOrder(orderId));
    }

    /**
     * 当前订单执行的操作
     */
    private List<OrderAction> getActionList(OrderEntity orderEntity) {
        List<OrderAction> actions = KefuOrderActionUtil.orderActions(orderEntity.getOrderStatus());
        // 新订单 待确认 待收货  -预约按钮
        if (Lists.asList(OrderStatusEnum.NEW_ORDER.value(), OrderStatusEnum.WAIT_CHECK.value(),OrderStatusEnum.WAIT_PICKUP.value(), OrderStatusEnum.WAIT_RECEIVE.value()).contains(orderEntity.getOrderStatus())) {
            this.orderReserveActionCtrl(actions, orderEntity);
        }
        //确认发货按钮
        if (Lists.asList(OrderStatusEnum.WAIT_CHECK.value(),OrderStatusEnum.WAIT_PICKUP.value()).contains(orderEntity.getOrderStatus())) {
            this.orderDeliveryActionCtrl(actions, orderEntity);
        }
        // 待收货 确认收货
        if (Lists.asList(OrderStatusEnum.WAIT_RECEIVE.value(),OrderStatusEnum.WAIT_PICKUP.value(), OrderStatusEnum.WAIT_CHECK.value()).contains(orderEntity.getOrderStatus())) {
            this.orderReceiveActionCtrl(actions, orderEntity);
        }
        // 待议价 待质检 退货按钮
        if (Lists.asList(OrderStatusEnum.WAIT_BARGAIN.value(), OrderStatusEnum.WAIT_QUALITY.value(),OrderStatusEnum.WAIT_PAY.value(),OrderStatusEnum.WAIT_RETURN.value()).contains(orderEntity.getOrderStatus())) {
            this.orderReturnActionCtrl(actions, orderEntity);
        }
        // 新订单 待确认1 待接单 待确认 待上门
        if (Lists.asList(OrderStatusEnum.NEW_ORDER.value(), OrderStatusEnum.WAIT_CHECK.value(), OrderStatusEnum.WAIT_ACCEPT.value(), OrderStatusEnum.WAIT_CONFIRM.value(), OrderStatusEnum.WAIT_VISIT.value()).contains(orderEntity.getOrderStatus())) {
            this.orderDispatchActionCtrl(actions, orderEntity);
        }
        //超时关闭按钮
        if (Lists.asList(OrderStatusEnum.NEW_ORDER.value(), OrderStatusEnum.WAIT_CHECK.value(), OrderStatusEnum.WAIT_RECEIVE.value(), OrderStatusEnum.WAIT_ACCEPT.value(), OrderStatusEnum.WAIT_CONFIRM.value(), OrderStatusEnum.WAIT_VISIT.value()).contains(orderEntity.getOrderStatus())) {
            this.orderTimeCloseActionCtrl(actions, orderEntity);
        }
        //收成打款
        if (Lists.asList(OrderStatusEnum.WAIT_PAY.value()).contains(orderEntity.getOrderStatus())) {
            this.orderPayActionCtrl(actions, orderEntity);
        }
        //完成订单
        if (Lists.asList(OrderStatusEnum.WAIT_PAY.value()).contains(orderEntity.getOrderStatus())) {
            this.orderCompleteActionCtrl(actions, orderEntity);
        }
        //重发能量
        if (Lists.asList(OrderStatusEnum.COMPLETE.value()).contains(orderEntity.getOrderStatus())) {
            this.sendEnergyActionCtrl(actions, orderEntity);
        }
        if(Objects.isNull(Contexts.getUserContext().getUserType())){
            return actions;
        }
        List<OrderAction> newActions = Lists.newArrayList();
        for (OrderAction action : actions) {
            newActions.add(action);
        }
        return newActions;
    }
    /**
     * 重置订单按钮
     */

    private void orderResetStatus(List<OrderAction> actions, OrderEntity orderEntity) {
        final BigDecimal prepaidPrice = orderEntity.getPrepaidPrice();
        final Date createTime = orderEntity.getCreateTime();
        final KefuOrderActionEnum action = KefuOrderActionEnum.RESET;
        if (prepaidPrice.compareTo(BigDecimal.ZERO) > 0) {
            return;
        }
        if (createTime.before(Dates.addMonths(Dates.getTimeNow(), -1))) {
            return;
        }


        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }


    /**
     * 预约按钮
     */
    private void orderReserveActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.RESERVE;

        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 确认发货按钮
     */
    private void orderDeliveryActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.DELIVERY;

        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 确认收货按钮
     */
    private void orderReceiveActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.RECEIVE;
        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 退货按钮
     */
    private void orderReturnActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.RETURN;
        final String platform = orderEntity.getPlatform();
        if (Objects.equals(platform, OrderPlatformEnum.COMMERCE.value())) {
            if (!Objects.equals(orderEntity.getOrderStatus(), OrderStatusEnum.WAIT_RETURN.value())) {
                return;
            }
        }
        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 关闭订单按钮
     */
    private void orderCloseActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.ClOSE;
        final String platform = orderEntity.getPlatform();
        if(!Lists.asList(OrderStatusEnum.NEW_ORDER.value(),OrderStatusEnum.WAIT_CHECK.value()).contains(orderEntity.getOrderStatus())){
            return;
        }
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }

    /**
     * 派单按钮
     */
    private void orderDispatchActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.DISPATCH;
        if (Objects.equals(Contexts.getUserContext().getUserType(), ContextUserType.ADMIN_PLATFORM.value())) {
            // 查询是否有上门订单
            if (Objects.equals(OrderTypeEnum.DOOR_RECYCLE.value(), orderEntity.getOrderType())) {
                actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
            }
        } else {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));

        }

    }


    /**
     * 超时关闭按钮
     */
    private void orderTimeCloseActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.TIMEOUTCLOSE;
        if (Objects.equals(Contexts.getUserContext().getUserType(), ContextUserType.ADMIN_PLATFORM.value())) {
            return;
        }
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }

    /**
     * 收成打款
     */
    private void orderPayActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.PAY;
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }

    /**
     * 完成订单
     */
    private void orderCompleteActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.COMPLETE;
        if(Lists.asList(OrderSourceEnum.CMCC_ONLINE.value()).contains(orderEntity.getOrderSource())){
            return;
        }
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }
    /**
     * 重发能量
     */
    private void sendEnergyActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.SENDENERGY;
        final String platform = orderEntity.getPlatform();
        if (Objects.equals(platform, OrderPlatformEnum.ALIPAY.value())){
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

}
